home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / asmbler.arc / CTLT.ASM < prev    next >
Assembly Source File  |  1988-11-19  |  8KB  |  201 lines

  1.         title   CTLT - Control-T Interrupt Handler
  2.         page    66,132
  3. ;***********************************************************************
  4. ;
  5. ; This routine works by trapping every interrupt 9 (KB_INT) generated by
  6. ; the keyboard and handled by the IBM BIOS, and after return from BIOS,
  7. ; inspecting the key which was typed and is now present in the keyboard
  8. ; ring buffer in the BIOS data segment (40h).  If it is the user interrupt
  9. ; character (normally a CTL-T), it is removed from the buffer and a status
  10. ; message is displayed on the console screen.
  11. ;
  12. ;                          *** N O T E ***
  13. ; This code depends upon buffer size, organization, and location,
  14. ; and pointers thereto, in the IBM BIOS data segment.  If a different keyboard interrupt
  15. ; handler is installed (e.g. one that increases buffer size), this code
  16. ; will probably fail!
  17. ;
  18. ; [02-Aug-84]   Nelson H.F. Beebe
  19. ;***********************************************************************
  20.  
  21.         .xlist
  22.         include ascii.inc
  23.         include dos.inc
  24.         .list
  25.  
  26.  
  27. keyvect segment at 0
  28.         org     9h*4                    ; BIOS KB_INT
  29. keyint  label   dword
  30. keyvect ends
  31.  
  32.  
  33. biosdata        segment at 40h
  34.                 org     1ah             ; See p. A-3 of IBM Tech. Ref. Manual
  35. buffer_head     dw      ?               ; pointer to head of keyboard buffer
  36.                                         ; -- address of first available char
  37. buffer_tail     dw      ?               ; pointer to tail of keyboard buffer
  38.                                         ; -- address of next empty slot
  39.                                         ; -- buffer is empty if head=tail
  40. kb_buffer       dw      16 dup (?)      ; room for 15 entries
  41.                 org     $-2
  42. kb_bufend       dw      ?               ; last word in buffer
  43. biosdata        ends       
  44.  
  45.  
  46. ;***********************************************************************
  47. ;
  48. ;       INIT_CODE - Code to load and initialize the handler.
  49. ;       Sets up DOS to keep all code before "LASTONE" label safe from
  50. ;       overlaying during system operation.
  51. ;
  52. ;***********************************************************************
  53.  
  54. code    segment para
  55.         org     100h                    ; for .COM file
  56.  
  57.         assume  cs:code,ds:code,ss:code
  58.         assume  es:biosdata
  59.  
  60. KEY     PROC    FAR
  61. start:
  62.         jmp     init_code               ; done only once at startup
  63.  
  64. key_call:                               ; Far JMP to keyboard interrupt
  65.         db      0eah
  66.         dw      0,0
  67.  
  68. test_msg db     "Control-T typed",0 
  69.  
  70. key_rtne:
  71.         sti                             ; turn interrupts back on
  72.  
  73.         push    ax                      ; save registers
  74.         push    bx
  75.         push    cx
  76.         push    dx
  77.         push    bp
  78.         push    ds
  79.         push    es
  80.         push    di
  81.         push    si
  82.  
  83.         mov     bx,cs
  84.         mov     ds,bx                   ; establish local DS = CS
  85.  
  86.         mov     bx,biosdata
  87.         mov     es,bx                   ; establish ES -> BIOSDATA
  88.  
  89. ; first call original KB_INT BIOS code to actually retrieve the character
  90. ; over the keyboard port and do its massive amount of bookkeeping
  91.  
  92.         pushf                           ; IBM keyboard proc expects interrupt 
  93.                                         ; call
  94.         mov     bx,offset key_call+1    ; get address of ROM code for keyboard
  95.         call    dword ptr [bx]          ; call ROM code
  96.  
  97. ;-----------------------------------------------------------------------
  98. ; enter critical section -- no interrupts allowed until character retrieved
  99. ; and buffer pointers updated
  100.  
  101.         cli                             ; interrupts off while char retrieved
  102.         mov     bx,es:buffer_tail       ; next available slot in buffer
  103.         dec     bx                      ; backup 2 bytes
  104.         dec     bx
  105.         cmp     bx,offset kb_buffer     ; backed up before start?
  106.         jae     nowrap                  ; no
  107.         mov     bx,offset kb_bufend     ; yes, move to end of buffer
  108. nowrap:
  109.         mov     ax,es:[bx]              ; char in al, scan code in ah
  110.         
  111.         cmp     al,.ctlt                ; CTL-T?
  112.         jne     done                    ; no
  113.  
  114.         mov     es:buffer_tail,bx       ; remove last character (the CTL-T)
  115.         sti                             ; interrupts back on
  116.  
  117. ; end critical section
  118. ;-----------------------------------------------------------------------
  119.  
  120.         cld                             ; set to read string forward
  121.         mov     si,offset test_msg      ; string to output on CTL-T
  122.         
  123.         mov     ah,$video_getvideostate
  124.         int     $video                  ; get current page in bh
  125.  
  126.         mov     ah,$video_getattrchar
  127.         int     $video                  ; get current attribute in ah
  128.         mov     bl,ah                   ; save current attribute
  129.  
  130. msg_loop:
  131.         lodsb                           ; byte to al, increment si
  132.         mov     dl,al                   ; message byte
  133.         test    dl,dl                   ; byte = 0?
  134.         jz      done                    ; yes, all done
  135.  
  136.         mov     ah,$video_settty        ; tty output (bl = current attribute)
  137.         int     $video                  ; character
  138.  
  139.         jmp     short msg_loop          ; keep printing
  140.  
  141. done:
  142.         sti                             ; interrupts back on if not yet done
  143.  
  144.         pop     si                      ; restore saved registers
  145.         pop     di
  146.         pop     es
  147.         pop     ds
  148.         pop     bp
  149.         pop     dx
  150.         pop     cx
  151.         pop     bx
  152.         pop     ax
  153.         iret                            ; return from interrupt
  154. KEY     ENDP
  155.  
  156. lastone:        ; all code after this label is freed to DOS after
  157.                 ; program initialization
  158.  
  159. copyrt  DB      'CONTROL-T Handler -- Version 1.0 -- '
  160.         DB      'Public Domain 1984 -- Nelson H.F. Beebe',13,10,'$'
  161.  
  162.  
  163.         assume  cs:code,ds:code,ss:code
  164.         assume  es:keyvect              ; 'VECTORS' is interrupt segment 0
  165. INIT_CODE PROC NEAR
  166.  
  167. ; Initialize KEYBOARD interrupt system
  168.         
  169. ;-----------------------------------------------------------------------
  170. ; enter critical section -- no interrupts while adjusting interrupt vector
  171. ;
  172. ;        cli                             ; interrupts off
  173.         mov     ax,keyvect              ; Get address to interrupt vector
  174.         mov     es,ax                   ; Save in ES
  175.         mov     ax,es:keyint            ; Get address to interrupt routine
  176.         mov     bx,offset key_call+1    ; Address to place to save vector
  177.         mov     [bx],ax                 ; Save interrupt address
  178.         mov     ax,es:keyint[2]         ; Get interrupt segment for routine
  179.         mov     [bx+2],ax               ; Save it too
  180.         mov     es:keyint,offset key_rtne       ; Now, replace with own address
  181.         mov     ax,cs                   ; Save segment in interrupt vector
  182.         mov     es:keyint[2],ax
  183.         sti                             ; interrupts back on
  184.  
  185. ; end critical section
  186. ;-----------------------------------------------------------------------
  187.  
  188. ; Now, print out acknowledgement to user monitor and exit
  189.  
  190.         mov     ax,cs                   ; Set up segment to this routine
  191.         mov     ds,ax
  192.         mov     dx,offset copyrt        ; Now, print out copyright message
  193.         mov     ah,$DOS_STROUT          ; DOS function to print string
  194.         int     $DOS                    ; Execute function interrupt
  195.         mov     dx,offset lastone       ; Save all code up to "LASTONE" label
  196.         int     27h                     ; No return needed
  197.  
  198. INIT_CODE ENDP
  199. code    ends
  200.         end     start
  201.